package com.chen10.gobang.service.impl;

import com.chen10.gobang.ai.model.AiMoveRequest;
import com.chen10.gobang.ai.model.AiMoveResponse;
import com.chen10.gobang.ai.GobangAIChooser;

import com.chen10.gobang.ai.model.Position;
import com.chen10.gobang.common.enums.game.AiFightResultEnum;
import com.chen10.gobang.common.utils.GameUtils;
import com.chen10.gobang.service.AiFightService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AiFightServiceImpl implements AiFightService {

    @Override
    public AiMoveResponse move(AiMoveRequest req) {
        int[][] board = req.getBoard();
        int row = req.getRow();
        int col = req.getCol();

        //判断玩家胜负
        List<int[]> winList = GameUtils.checkWin(board,row,col);
        if (winList != null) {
            return new AiMoveResponse(AiFightResultEnum.WIN, winList, null);
        }
        // 调用AI计算落子位置
        Position aiDecisionPos = GobangAIChooser.calculateMove(req);
        //更新棋盘
        board[aiDecisionPos.getRow()][aiDecisionPos.getCol()] = req.getAiColor();
        //判断AI胜负
        winList = GameUtils.checkWin(board, aiDecisionPos.getRow(), aiDecisionPos.getCol());
        if( winList != null) {
            return new AiMoveResponse(AiFightResultEnum.LOSE, winList, aiDecisionPos);
        }
        return new AiMoveResponse(AiFightResultEnum.CONTINUE, null, aiDecisionPos);
    }
}
